﻿using FSM.IBll;
using FSM.IDal;
using FSM.Model;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Util.Config;
using System.Linq;

namespace FSM.Bll
{
    public class SysSettingBll : ISysSettingBll
    {
        #region 模块

        private readonly ISysSettingDal _dal;

        private readonly IRelUserRoleBll _relUserRoleBll;

        private readonly IRelMenuActionBll _relMenuAction;

        private readonly ISysParamSubBll _paramBll;

        #endregion

        #region 构造函数

        public SysSettingBll(ISysSettingDal dal, ISysParamSubBll paramBll, IRelUserRoleBll relUserRoleBll, IRelMenuActionBll relMenuAction)
        {
            _dal = dal;
            _relUserRoleBll = relUserRoleBll;
            _relMenuAction = relMenuAction;
            _paramBll = paramBll;
        }

        #endregion

        #region 权限设定

        /// <summary>
        /// 获得用户对指定菜单拥有的操作权限按钮
        /// </summary>
        /// <param name="user_no">用户编号</param>
        /// <param name="menu_no">菜单编号</param>
        /// <returns></returns>
        public List<sys_action> ListPowerAction(string user_no, string menu_no)
        {
            //判断用户是否超级管理员组的成员,如果是超管理员，则直接返回True，他拥有所有权限
            bool isAdmin = _relUserRoleBll.IsAdministrators(user_no);
            if (isAdmin)
            {
                return _relMenuAction.ListByMenuNo(menu_no);
            }
            return _dal.ListPowerAction(user_no, menu_no);
        }


        /// <summary>
        /// 是否拥有指定菜单的操作权限
        /// </summary>
        /// <param name="user_no">用户编号</param>
        /// <param name="menu_no">菜单编号</param>
        /// <param name="action_no">动作编号</param>
        /// <returns></returns>
        public bool HasOperationPower(string user_no, string menu_no, string action_no)
        {
            //判断用户是否超级管理员组的成员,如果是超管理员，则直接返回True，他拥有所有权限
            bool isAdmin = _relUserRoleBll.IsAdministrators(user_no);
            if (isAdmin)
            {
                return true;
            }

            //if (action_no.ToLower() == "view")
            //{
            //    //如果是查看权限，要看用户是否有此模块的权限即可，不用查看对应的动作权限
            //    return _dal.HasViewMenuPower(user_no, menu_no);
            //}
            //else
            //{
            return _dal.HasOperationPower(user_no, menu_no, action_no);
            //}
        }

        #endregion

        #region 扩展设定

        public static void SetPrintConfig(IServiceCollection services)
        {
            var app = services.BuildServiceProvider();
            ISysParamSubBll tempParamBll = app.GetRequiredService<ISysParamSubBll>();
            var list = tempParamBll.List(6, "SugarSQL", null);
            if (list.Count > 0)
            {
                CommonSetting.PrintSqlOfSqlSugar = list.First().sub_value == "Y";
            }
            else
            {
                CommonSetting.PrintSqlOfSqlSugar = false;
            }
        }

        #endregion
    }
}
